import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.FileNotFoundException;
import java.io.IOException;
//import java.util.Calendar;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.DataLine.Info;

public class AudioFileReader {

	FileInputStream waveStream;
	AudioInputStream audioInputStream = null;
	AudioFormat audioFormat;
	Info info;
	//SourceDataLine dataLine = null;

	int frameSize = 919;
	long frameData[] = new long[1444 * 918];
	long prev = 0;
	int frameCtr = 0;

	final int buffer_size_bytes = 10 * 1024;

	public AudioFileReader(String in_wav_file)
			throws UnsupportedAudioFileException, IOException,
			LineUnavailableException {
		waveStream = new FileInputStream(in_wav_file);

		audioInputStream = AudioSystem.getAudioInputStream(this.waveStream);

		// Obtain the information about the AudioInputStream
		audioFormat = audioInputStream.getFormat();
		info = new Info(SourceDataLine.class, audioFormat);

		// opens the audio channel
		// dataLine = (SourceDataLine) AudioSystem.getLine(info);
		// dataLine.open(audioFormat, (buffer_size_bytes));

		// Starts the music :P
		// dataLine.start();
	}

	void readAndCalcStats(FileStats stats) throws IOException {
		// int type=1;
		int readBytes = 0;
		byte[] audioBuffer = new byte[buffer_size_bytes];
		int counter = 0;
		int dataCounter = 0;

		do {

			readBytes = audioInputStream.read(audioBuffer, 0,
					audioBuffer.length);

			if (readBytes >= 0) {

				long data = 0;
				for (int i = 0; i < readBytes; i += 2) {
					data = Math.abs(((int) ((audioBuffer[i + 1] << 8)))
							| ((int) (audioBuffer[i])));

					if (dataCounter < 919) {
						frameData[frameCtr] += data;
						counter++;
						prev = data;
						dataCounter++;
					} else if (dataCounter == 919) {
						frameCtr++;
						//System.out.println(frameCtr);
						// out.println(frameData[frameCtr - 1]);
						// out.flush();
						frameData[frameCtr] += prev;
						dataCounter++;
						frameData[frameCtr] += data;
					} else if (dataCounter > 919 && dataCounter < 1838) {
						frameData[frameCtr] += data;
						counter++;
						prev = data;
						dataCounter++;
					} else if (dataCounter == 1838) {
						frameCtr++;
						//System.out.println(frameCtr);

						// out.println(frameData[frameCtr - 1]);
						frameData[frameCtr] += prev;
						dataCounter++;
						frameData[frameCtr] += data;
					}

					else if (dataCounter > 1838 && dataCounter < 2757) {
						frameData[frameCtr] += data;
						counter++;
						prev = data;
						dataCounter++;
					} else if (dataCounter == 2757) {
						frameCtr++;
						//System.out.println(frameCtr);
						// out.println(frameData[frameCtr - 1]);
						frameData[frameCtr] += prev;
						dataCounter++;
						frameData[frameCtr] += data;
					}

					else if (dataCounter > 2757 && dataCounter < 3675) {
						frameData[frameCtr] += data;
						counter++;
						prev = data;
						dataCounter++;
					} else if (dataCounter == 3675) {
						frameCtr++;
						//System.out.println(frameCtr);
						// out.println(frameData[frameCtr - 1]);
						dataCounter = 0;

					}
				}
			}

		} while (readBytes >= 0);
		
		for(int i = 0; i < stats.maxFrameID; i++)
		{
			if(frameData[i] > stats.maxAud)
				stats.maxAud = frameData[i];
			if(frameData[i] < stats.minAud)
				stats.minAud = frameData[i];
			
			frameInfo f = stats.frameInfoList.get(i);
			f.audio = frameData[i];

		}
	}
}
